package com.site.wisdom_construction.controller.integralSupermarketModule;

import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.integralSupermarketModule.ConversionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/17
 * @Description
 */
@Api(tags = "兑换记录接口")
@RestController
@RequestMapping("ConversionRecord")
@CrossOrigin
public class ConversionRecordController {
    @Autowired
    private ConversionRecordService conversionRecordService;

    @ApiOperation(value = "增加兑换记录", notes = "测试数据:")
    @PostMapping("/insertConversionRecord")
    public JsonResult insertConversionRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = conversionRecordService.insertConversionRecord(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入兑换记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入兑换记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除兑换记录信息", notes = "测试数据:")
    @PostMapping("/deleteConversionRecord")
    public JsonResult deleteConversionRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = conversionRecordService.deleteConversionRecord(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除兑换记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除兑换记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改兑换记录", notes = "测试数据:")
    @PostMapping("/updateConversionRecord")
    public JsonResult updateConversionRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = conversionRecordService.updateConversionRecord(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改兑换记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改兑换记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询兑换记录", notes = "测试数据:")
    @PostMapping("/findConversionRecordByPage")
    public Page findConversionRecordByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(conversionRecordService.Total(map));
        page.setItems(conversionRecordService.findConversionRecordByPage(map));
        return page;
    }

    @ApiOperation(value = "分页模糊查询个人兑换记录", notes = "测试数据:")
    @PostMapping("/findConversionRecordByPagePerson")
    public Page findConversionRecordByPagePerson(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(conversionRecordService.totalByPerson(map));
        page.setItems(conversionRecordService.findConversionRecordByPagePerson(map));
        return page;
    }
}
